﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WPFDemo
{
    public class ClassDemo2
    {

        public void Run()
        {
            var p = new Person()
            {
                Id = 13,
                Name = "csb",
                Age = 18,
                Gender = Gender.male,
                Class = "3A"
            };

            MyPropertyEditor(p);

            var btn = new Button() { Content = "Check" };
            btn.Click += (_, _) =>
            {
                //p.Dump();
            };
        }

       

        string MyPropertyEditor(object selectedObject)
        {
            //selectedObject.GetType().GetProperties().Select(pi => GetEditor(pi, selectedObject)).ToList();

            var res = selectedObject.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(pi =>
                {
                    var attr = pi.GetCustomAttribute<TestAttribute>();
                    if (attr is not null) return attr.Browsable;
                    return true;
                })
                .Select(pi => new { key = pi.Name, value = pi.GetValue(selectedObject) })
                .Select(o => $"{o.key}:{o.value}");

            return string.Join(Environment.NewLine, res);

        }

        private UIElement GetEditor(PropertyInfo pi,object obj)
        {
            var panel = new StackPanel();
            TextElement.SetFontSize(panel, 20);
            panel.Children.Add(new TextBlock { Text = pi.Name });
            if (pi.PropertyType == typeof(string))
            {
                var box = new TextBox();
                box.Text = pi.GetValue(obj).ToString();
                box.TextChanged += (sender, e) =>
                {
                    pi.SetValue(obj, box.Text);
                };
                panel.Children.Add(box);
            }else if (pi.PropertyType == typeof(int))
            {
                var slider = new Slider();
                slider.Maximum = 100;
                slider.Value = (int)pi.GetValue(obj);
                panel.Children.Add(slider);
            }
            else if (pi.PropertyType == typeof(Gender))
            {
                var combo = new ComboBox();
                combo.ItemsSource = Enum.GetValues<Gender>();
                combo.SelectedItem = (Gender)pi.GetValue(obj);
                panel.Children.Add(combo);
            }
            return panel;
        }
    }

    class Person
    {
        [TestAttribute(false, Tag ="123")]
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public Gender Gender { get; set; }
        public string Class { get; set; }
    }

    public enum Gender
    {
        Female,
        male,
    }

    /// <summary>
    /// 自定义特性
    /// </summary>
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]
    class TestAttribute:Attribute
    {
        public bool Browsable { get; set; }
        public string Tag { get; set; } 

        public TestAttribute(bool b)
        {
            Browsable = b;
        }

    }


}
